// pages/auth/index.js
import { request } from '../../request/index'
import { login } from '../../utils/asyncWx'
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({

  data: {

  },

  onLoad: function (options) {

  },

  // 获取用户信息
  async handleGetUserInfo (e) {
    // console.log(e)
    try {
      // 1 获取用户信息
      const { encryptedData, iv, rawData, signature } = e.detail
      // 2 获取用户登录的 code
      const { code } = await login()

      const params = { encryptedData, iv, rawData, signature, code }

      // 3 获取用户 token 
      const { token } = request({ url: "/users/wxlogin", data: params, method: "post" })
      // console.log(token);

      // 4 把token 存储到本地
      wx.setStorageSync("token", token);

      // 5 跳转回上一个页面
      wx.navigateBack({
        delta: 1
      })
    } catch (error) {
      console.log(error)
    }


  }
})